'use strict';

const { Controller } = require('egg');
const moment = require('moment');
const statusList = [
    { label: '待执行', value: 0 },
    { label: '执行中', value: 1 },
    { label: '成功', value: 2 },
    { label: '失败', value: 3 }
]

/**
 * @Controller 任务接口
 */

class TaskController extends Controller {
    /**
     * @summary 任务分页查询
     * @description 任务分页查询
     * @router post /api/task/getList
     * @response 200 baseResponse 成功
     */
    async List(){
        const { ctx } = this;
        const payload = ctx.request.body || {};
        const page_index = payload.page_index || 1;
        const page_size = payload.page_size||10;

        let data = await ctx.model.Task.findAndCountAll({
            offset: (page_index - 1) * page_size,
            limit: page_size,
            where: {},
            order: [['createdAt', 'DESC']],
        })
        let list = data.rows.map(item=>{
            item.dataValues.createdAt = moment(item.dataValues.createdAt).format('YYYY-MM-DD HH:mm:ss')
            item.status = this.transForm(statusList, item.status)
            return item;
        })
        ctx.helper.success({ ctx, res:{ list, total: data.count } })
    }

    transForm(arr, value){
        let data = arr.filter(item=>item.value==value)
        if(data.length>0){
            return data[0].label
        }
        return ''
    }

}

module.exports = TaskController;